package com.zmc.coupon.listener;
import com.rabbitmq.client.Channel;
import com.zmc.common.constant.QueueConstant;
import com.zmc.coupon.application.CouponApplicationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author：zmc
 * @Date：2024/12/17 22:19
 */

@Component
public class CouponConsumer {
    public static final Logger log = LoggerFactory.getLogger(CouponConsumer.class);

    @Autowired
    private CouponApplicationService couponApplicationService;
    /**
     * 返优惠卷
     * */
    @RabbitListener(queues = QueueConstant.COUPON_RETURN_QUEUE)
    @RabbitHandler
    public void couponReturn(String orderSn, Message message, Channel channel) throws IOException {
        long msgTag = message.getMessageProperties().getDeliveryTag();
        log.info("mq -> couponReturn，接收到消息:{}, msgTag:{}", orderSn, msgTag);
        couponApplicationService.returnCoupon(orderSn);
        channel.basicAck(msgTag, false);
    }
}
